home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Archive / Graphics / QD3D / rollercoaster / Sources / Utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  10.5 KB  |  346 lines  |  [TEXT/dosa]

  1. /*
  2.     File:        Utils.c
  3.     
  4.     Contains:    Utility functions
  5.     
  6.     Written by:    Scott Kuechle, based on original Gerbils code by Brian Greenstone
  7.  
  8.     Copyright:    © 1998 by Apple Computer, Inc. All rights reserved
  9.     
  10.     Change History (most recent first)
  11.     
  12.         <1>        9/1/98        srk        first file
  13.  
  14.  
  15. */
  16. /************************************************************
  17. *                                                           *
  18. *    INCLUDE FILES                                          *
  19. *                                                           *
  20. *************************************************************/
  21.  
  22. #include "Utils.h"
  23.  
  24. /************************************************************
  25. *                                                           *
  26. *    FUNCTION PROTOTYPES                                    *
  27. *                                                           *
  28. *************************************************************/
  29.  
  30. #if TARGET_OS_MAC
  31.     void Utils_Macintosh_DisplayMsg(char *msg);
  32. #else if TARGET_OS_WIN32
  33.     void Utils_Win32_DisplayMsg(char *msg);
  34.  
  35. #endif
  36.  
  37. /************************************************************
  38. *                                                           *
  39. *    CONSTANTS                                              *
  40. *                                                           *
  41. *************************************************************/
  42.  
  43. #if TARGET_OS_MAC
  44.     #define MsgDialogRsrcID        129
  45.     #define MsgItemID            3    
  46. #endif
  47.  
  48. /************************************************************
  49. *                                                           *
  50. *    FUNCTION:  Utils_MyRandomLong                          *
  51. *                                                           *
  52. *    PURPOSE:   Our own random number generator which       *
  53. *               generates a random long value               *
  54. *                                                           *
  55. *************************************************************/
  56.  
  57. unsigned long Utils_MyRandomLong(void)
  58. {
  59. unsigned long seed0 = 0, seed1 = 0, seed2 = 0;
  60.  
  61.   return seed2 ^= (((seed1 ^= (seed2>>5)*1568397607UL)>>7)+
  62.                    (seed0 = (seed0+1)*3141592621UL))*2435386481UL;
  63. }
  64.  
  65.  
  66. /************************************************************
  67. *                                                           *
  68. *    FUNCTION:  Utils_RotatePoint                           *
  69. *                                                           *
  70. *    PURPOSE:   Rotates the given point around the y-axis   *
  71. *               0,0 by angle radians                        *
  72. *                                                           *
  73. *************************************************************/
  74.  
  75. void Utils_RotatePoint(TQ3Point3D *point, float yangle)
  76. {
  77. float    x,y,z;
  78.  
  79.     x = point->x;
  80.     y = point->y;
  81.     z = point->z;
  82.     
  83.                 /* ROTATE ON Y AXIS */
  84.                 
  85.     point->z = z * cos(yangle) - x * sin(yangle);
  86.     point->x = z * sin(yangle) + x * cos(yangle);
  87. }
  88.  
  89. /************************************************************
  90. *                                                           *
  91. *    FUNCTION:  Utils_AngleBetweenVectors                   *
  92. *                                                           *
  93. *    PURPOSE:   Calculate the angle in radians between 2    *
  94. *               3D vectors                                  *
  95. *                                                           *
  96. *************************************************************/
  97.  
  98. float Utils_AngleBetweenVectors(TQ3Vector3D v1, TQ3Vector3D v2)
  99. {
  100. float    dot,angle;
  101.  
  102.     Q3Vector3D_Normalize(&v1,&v1);    /* make sure they're normalized */
  103.     Q3Vector3D_Normalize(&v2,&v2);
  104.  
  105.     dot = Q3Vector3D_Dot(&v1,&v2);    /* the dot product is the cosine of the angle between the 2 vectors */
  106.     angle = acos(dot);                /* get arc-cosine to get the angle out of it */
  107.     
  108.     return(angle);
  109. }
  110.  
  111. /************************************************************
  112. *                                                           *
  113. *    FUNCTION:  Utils_DisplayErrorMsg                       *
  114. *                                                           *
  115. *    PURPOSE:   Display routine for error messages          *
  116. *                                                           *
  117. *************************************************************/
  118. void Utils_DisplayErrorMsg(char *msg)
  119. {
  120.     #if TARGET_OS_MAC
  121.         Utils_Macintosh_DisplayMsg(msg);
  122.     #else if TARGET_OS_WIN32
  123.         Utils_Win32_DisplayMsg(msg);
  124.     #endif
  125. }
  126.  
  127. /************************************************************
  128. *                                                           *
  129. *    FUNCTION:  Utils_DisplayFatalErrorMsg                  *
  130. *                                                           *
  131. *    PURPOSE:   Display routine for error messages          *
  132. *                                                           *
  133. *************************************************************/
  134. void Utils_DisplayFatalErrorMsg(char *msg)
  135. {
  136.     #if TARGET_OS_MAC
  137.         Utils_Macintosh_DisplayMsg(msg);
  138.         ExitToShell();
  139.     #else if TARGET_OS_WIN32
  140.         Utils_Win32_DisplayMsg(msg);
  141.         ExitProcess(0); 
  142.     #endif
  143. }
  144.  
  145.  
  146. /************************************************************
  147. *                                                           *
  148. *    FUNCTION:  Utils_Macintosh_DisplayMsg                  *
  149. *                                                           *
  150. *    PURPOSE:   Displays Macintosh error messages           *
  151. *                                                           *
  152. *************************************************************/
  153.  
  154. #if TARGET_OS_MAC
  155. void Utils_Macintosh_DisplayMsg(char *msg)
  156. {
  157.     DialogPtr theDlog;
  158.     Handle item = NULL;
  159.     Rect box;
  160.  
  161.         theDlog = GetNewDialog(MsgDialogRsrcID, NULL, (WindowPtr)-1);
  162.         if (theDlog != NULL)
  163.         {
  164.             short itemType;
  165.             
  166.                 GetDialogItem(theDlog, MsgItemID, &itemType, &item, &box);
  167.                 if (item != NULL)
  168.                 {
  169.                     short itemHit;
  170.                     
  171.                         SetDialogItemText(item, c2pstr(msg));
  172.                         ModalDialog(NULL, &itemHit);
  173.                         DisposeDialog(theDlog);
  174.                         p2cstr((StringPtr)msg);    /* restore C-string */
  175.                 }
  176.         }
  177.         else    /* program resources can't be found, so use macsbug */
  178.         {
  179.             DebugStr(c2pstr(msg));
  180.             p2cstr((StringPtr)msg);    /* restore C-string */
  181.         }
  182. }
  183. #endif
  184.  
  185. /************************************************************
  186. *                                                           *
  187. *    FUNCTION:  Utils_Win32_DisplayMsg                      *
  188. *                                                           *
  189. *    PURPOSE:   Displays error messages for Win95/NT sample *
  190. *               code                                        *
  191. *                                                           *
  192. *************************************************************/
  193.  
  194. #if TARGET_OS_WIN32
  195. void Utils_Win32_DisplayMsg(char *msg)
  196. {
  197.     /* Display the string. */
  198.  
  199.     MessageBox( NULL, msg, "Error", MB_OK|MB_ICONINFORMATION );
  200. }
  201.  
  202.  
  203. /************************************************************
  204. *                                                           *
  205. *    FUNCTION:  Utils_Win32_DisplayMsg                      *
  206. *                                                           *
  207. *    PURPOSE:   Displays error messages for Win95/NT sample *
  208. *               code                                        *
  209. *                                                           *
  210. *************************************************************/
  211.  
  212.  
  213. DWORD Utils_Win32_BuildCurDirPath(LPTSTR path, LPTSTR filename)
  214. {
  215.     char szAppPathHold[MAX_PATH];
  216.     DWORD nSize = MAX_PATH, len;
  217.     char *ndirLocal;
  218.     int count;
  219.  
  220.         path[0] = 0;
  221.  
  222.          len = GetModuleFileName( NULL, szAppPathHold, nSize);
  223.  
  224.         if (len != 0)
  225.         {
  226.              ndirLocal = strrchr( szAppPathHold, '\\' );
  227.              count = ndirLocal - szAppPathHold + 1;
  228.              strncat (path, szAppPathHold, count);
  229.  
  230.                 /* tack filename onto path to current directory */
  231.             strcat (path, filename );
  232.  
  233.             return ERROR_SUCCESS;
  234.         }
  235.         else
  236.         {
  237.             return (GetLastError());
  238.         }
  239. }
  240.  
  241.  
  242.  
  243. /************************************************************
  244. *                                                           *
  245. *    FUNCTION:  Utils_Win32_GetPicFromFile                  *
  246. *                                                           *
  247. *    PURPOSE:   Returns a Picture Handle for a PICT file    *
  248. *                                                           *
  249. *                                                           *
  250. *************************************************************/
  251.  
  252.  
  253. ComponentResult Utils_Win32_GetPicFromFile(LPTSTR        filePath,
  254.                                             PicHandle    *picH,
  255.                                             Rect        *picRect)
  256. {
  257.     FSSpec                    fsspec;
  258.     OSErr                    err;
  259.     GraphicsImportComponent    gi;
  260.     ComponentResult            result;
  261.  
  262.         *picH = NULL;
  263.         err = NativePathNameToFSSpec((char *)filePath, &fsspec, 0);
  264.  
  265.         if (err == noErr)
  266.         {
  267.             err = GetGraphicsImporterForFile(&fsspec, &gi);
  268.             if (err == noErr)
  269.             {
  270.                 result = GraphicsImportGetAsPicture(gi, picH);
  271.                 if (result == noErr)
  272.                 {
  273.                     return (GraphicsImportGetNaturalBounds(gi, picRect));
  274.                 }
  275.             }
  276.         }
  277.  
  278.         return err;
  279. }
  280.  
  281. /************************************************************
  282. *                                                           *
  283. *    FUNCTION:  Utils_Win32_GetPicFromFile                  *
  284. *                                                           *
  285. *    PURPOSE:   Returns a Picture Handle for a PICT file    *
  286. *                                                           *
  287. *                                                           *
  288. *************************************************************/
  289.  
  290. Boolean Utils_Win32_DoesFileExist(LPTSTR filePath)
  291. {
  292.     HANDLE        fileHndl = NULL;
  293.     Boolean        exists;
  294.  
  295.             /* ATTEMPT TO OPEN FILE */
  296.         fileHndl = CreateFile(filePath,
  297.                             GENERIC_READ,
  298.                             FILE_SHARE_READ,    /* share mode */
  299.                             NULL,
  300.                             OPEN_EXISTING,
  301.                             FILE_ATTRIBUTE_NORMAL,
  302.                             NULL);
  303.         if ((fileHndl == NULL) || (fileHndl == INVALID_HANDLE_VALUE))
  304.         {
  305.             exists = FALSE;
  306.         }
  307.         else
  308.         {
  309.             exists = TRUE;
  310.             CloseHandle(fileHndl);
  311.         }
  312.  
  313.         return exists;
  314. }
  315.  
  316. #endif
  317.  
  318. /************************************************************
  319. *                                                           *
  320. *    FUNCTION:  Utils_Mac_GetPictForTexture                 *
  321. *                                                           *
  322. *    PURPOSE:   Returns a Picture Handle for a PICT rsrc    *
  323. *                                                           *
  324. *                                                           *
  325. *************************************************************/
  326.  
  327. #if TARGET_OS_MAC
  328. void Utils_Mac_GetPictForTexture(short         resourceID,
  329.                                 PicHandle    *picH,
  330.                                 Rect        *picRect)
  331. {
  332.     *picH = GetPicture(resourceID);
  333.     if (*picH != NULL)
  334.     {
  335.         PicHandle pH;
  336.         
  337.             pH = *picH;
  338.             MacSetRect(picRect,
  339.                          (**pH).picFrame.left,
  340.                          (**pH).picFrame.top,
  341.                          (**pH).picFrame.right,
  342.                          (**pH).picFrame.bottom);
  343.     }
  344. }
  345. #endif
  346.